#include "BlazeSample.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	BeSystem* pSystem = new BeSystem;
	BeUISystem*	pUISystem = new BeUISystem;
	BeScriptSystem* pScriptSystem = new BeScriptSystem;

	BeLibrary* pAudioLibrary	= pSystem->GetLibrarySystem()->Load("./BlazePluginALAudio_d.dll");
	BeLibrary* pRenderLibrary	= pSystem->GetLibrarySystem()->Load("./BlazePluginGLRender_d.dll");
	BeLibrary* pNetworkLibrary	= pSystem->GetLibrarySystem()->Load("./BlazePluginTTNetwork_d.dll");

	if (pAudioLibrary != NULL)
	{
		INSTALLPLUGIN pFunc = (INSTALLPLUGIN)pAudioLibrary->GetProc("InstallPlugin");

		if (pFunc != NULL)
			pSystem->SetAudioSystem((BeAudioSystem*)pFunc());
	}

	if (pRenderLibrary != NULL)
	{
		INSTALLPLUGIN pFunc = (INSTALLPLUGIN)pRenderLibrary->GetProc("InstallPlugin");

		if (pFunc != NULL)
			pSystem->SetRenderSystem((BeRenderSystem*)pFunc());
	}

	if (pNetworkLibrary != NULL)
	{
		INSTALLPLUGIN pFunc = (INSTALLPLUGIN)pNetworkLibrary->GetProc("InstallPlugin");

		if (pFunc != NULL)
			pSystem->SetNetworkSystem((BeNetworkSystem*)pFunc());
	}

	pSystem->SetScriptSystem(pScriptSystem);
	pSystem->SetUISystem(pUISystem);

	pSystem->GetScriptSystem()->SetSystem(pSystem);
	pSystem->GetUISystem()->SetSystem(pSystem);
	pSystem->GetAudioSystem()->SetSystem(pSystem);
	pSystem->GetRenderSystem()->SetSystem(pSystem);
	pSystem->GetNetworkSystem()->SetSystem(pSystem);

	pSystem->GetScriptSystem()->Install();
	pSystem->GetUISystem()->Install();
	pSystem->GetAudioSystem()->Install();
	pSystem->GetRenderSystem()->InstallRenderWindow(0);
	pSystem->GetNetworkSystem()->Install();

	pSystem->Install();
	
	pSystem->GetRenderSystem()->SetWindowWidth(800);
	pSystem->GetRenderSystem()->SetWindowHeight(600);
	pSystem->GetRenderSystem()->SetFullscreen(false);
	
	unsigned long ulLastTime = GetTickCount();
	unsigned long ulFrame = 0;

	while (1)
	{
		pSystem->GetRenderSystem()->MessageLoop();

		pSystem->GetRenderSystem()->BeginRender(BeColor(100, 100, 100, 100));
		pSystem->GetRenderSystem()->SetFog(EFT_LINEAR, BeColor(200, 100, 50, 150), 0.5f, 0.3f, 0.7f);
		pSystem->GetRenderSystem()->EndRender();

		if (GetTickCount() - ulLastTime >= 1000)
		{
			ulLastTime = GetTickCount();
			ulFrame = 0;
		}
		else
		{
			ulFrame += 1;
		}
	}

	return 0;
}